using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using FarPoint.Win.Spread;
using Spread.Common.DataStore.DataSourceEntities;
using FarPoint.Win;
using FarPoint.Win.Spread.Model;

namespace Spread.ConditionalFormat
{
    public partial class ConditionalRules : BaseSampleControl
    {
        public ConditionalRules()
        {
            InitializeComponent();
            _cbOperator.DataSource = Enum.GetNames(typeof(ComparisonOperator));

            //Set Data
            var rnd = new Random();
            fpSpread1_Sheet1.SetClipValue(0, 0, 1, 5, "Student ID\tStudent Name\tEnglish\tMathematics\tScience");
            var students = new StudentDashboardDataSource().GetAllStudents();
            foreach (var student in students)
            {
                var id = student.ID;
                fpSpread1_Sheet1.Cells[id, 0].Value = id;
                fpSpread1_Sheet1.Cells[id, 1].Value = student.Name;
                fpSpread1_Sheet1.Cells[id, 2].Value = rnd.NextDouble() > 0.3 ? rnd.Next(40, 99) : rnd.Next(10, 40);
                fpSpread1_Sheet1.Cells[id, 3].Value = rnd.NextDouble() > 0.3 ? rnd.Next(40, 99) : rnd.Next(10, 40);
                fpSpread1_Sheet1.Cells[id, 4].Value = rnd.NextDouble() > 0.3 ? rnd.Next(40, 99) : rnd.Next(10, 40);
            }

            fpSpread1_Sheet1.Rows.Default.Height = 21;
            fpSpread1_Sheet1.Columns.Default.Width = 120;
            fpSpread1_Sheet1.Cells[0, 0, students.Count, 4].Border = new LineBorder(Color.Gray);
            fpSpread1_Sheet1.Cells[0, 0, 0, 4].BackColor = Color.Black;
            fpSpread1_Sheet1.Cells[0, 0, 0, 4].ForeColor = Color.White;
            fpSpread1_Sheet1.Cells[0, 0, 0, 4].Font = new Font("Segoe UI", 10, FontStyle.Bold);

            //Set Conditional Rules
            var lowMarksRule = new BetweenValuesConditionalFormattingRule(false, 1, false, 33, false);
            lowMarksRule.BackColor = Color.FromArgb(100, Color.LightPink);
            lowMarksRule.ForeColor = Color.Red;

            var avgMarksRule = new BetweenValuesConditionalFormattingRule(false, 34, false, 75, false);
            avgMarksRule.BackColor = Color.FromArgb(50, Color.LightGreen);
            avgMarksRule.ForeColor = Color.DarkGreen;

            var highMarksRule = new BetweenValuesConditionalFormattingRule(false, 76, false, 100, false);
            highMarksRule.BackColor = Color.FromArgb(100, Color.LightBlue);
            highMarksRule.ForeColor = Color.DarkBlue;

            fpSpread1_Sheet1.SetConditionalFormatting(1, 2, students.Count, 3, lowMarksRule, avgMarksRule, highMarksRule);
        }

        private void _bAddRule_Click(object sender, EventArgs e)
        {
            if (_cbOperator.SelectedIndex >= 0)
            {
                var compareOprtr = (ComparisonOperator)Enum.Parse(typeof(ComparisonOperator), _cbOperator.SelectedItem.ToString());
                var range = fpSpread1_Sheet1.GetSelection(0);
                if (range != null)
                    FarPoint.Win.Spread.ConditionalFormat.ShowConditionalFormatDialog(this.ParentForm, fpSpread1, compareOprtr, range);
                else
                {
                    var activeCell = fpSpread1_Sheet1.ActiveCell;
                    FarPoint.Win.Spread.ConditionalFormat.ShowConditionalFormatDialog(this.ParentForm, fpSpread1, compareOprtr, new CellRange(activeCell.Row.Index, activeCell.Column.Index, 1, 1));
                }
            }
        }

        private void _bClearRule_Click(object sender, EventArgs e)
        {
            var range = fpSpread1_Sheet1.GetSelection(0);
            if (range != null)
            {
                fpSpread1_Sheet1.ClearConditionalFormating(range.Row, range.Column, range.RowCount, range.ColumnCount);
            }
        }

        private void _bViewRules_Click(object sender, EventArgs e)
        {
            ConditionalFormattingUtils.ShowConditionalFormattingManagerForm(this.ParentForm, fpSpread1);
        }
    }
}


//
//ConditionalRules.Designer.cs
//


namespace Spread.ConditionalFormat
{
    partial class ConditionalRules
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread1_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this._lbOperator = new System.Windows.Forms.Label();
            this._cbOperator = new System.Windows.Forms.ComboBox();
            this._bAddRule = new System.Windows.Forms.Button();
            this._bClearRule = new System.Windows.Forms.Button();
            this._bViewRules = new System.Windows.Forms.Button();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this._bClearRule);
            this.pnlControls.Controls.Add(this._bAddRule);
            this.pnlControls.Controls.Add(this._bViewRules);
            this.pnlControls.Controls.Add(this.splitContainer1);
            // 
            // fpSpread1
            // 
            this.fpSpread1.AccessibleDescription = "";
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread1_Sheet1});
            this.fpSpread1.Size = new System.Drawing.Size(477, 487);
            this.fpSpread1.TabIndex = 0;
            // 
            // fpSpread1_Sheet1
            // 
            this.fpSpread1_Sheet1.Reset();
            this.fpSpread1_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread1_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread1_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // splitContainer1
            // 
            this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.splitContainer1.Location = new System.Drawing.Point(21, 15);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this._lbOperator);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this._cbOperator);
            this.splitContainer1.Size = new System.Drawing.Size(122, 36);
            this.splitContainer1.SplitterDistance = 40;
            this.splitContainer1.TabIndex = 0;
            // 
            // _lbOperator
            // 
            this._lbOperator.AutoSize = true;
            this._lbOperator.Dock = System.Windows.Forms.DockStyle.Fill;
            this._lbOperator.Location = new System.Drawing.Point(0, 0);
            this._lbOperator.Name = "_lbOperator";
            this._lbOperator.Size = new System.Drawing.Size(54, 13);
            this._lbOperator.TabIndex = 0;
            this._lbOperator.Text = "Operator :";
            // 
            // _cbOperator
            // 
            this._cbOperator.Dock = System.Windows.Forms.DockStyle.Fill;
            this._cbOperator.FormattingEnabled = true;
            this._cbOperator.Location = new System.Drawing.Point(0, 0);
            this._cbOperator.Name = "_cbOperator";
            this._cbOperator.Size = new System.Drawing.Size(78, 21);
            this._cbOperator.TabIndex = 0;
            // 
            // _bAddRule
            // 
            this._bAddRule.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._bAddRule.Location = new System.Drawing.Point(21, 57);
            this._bAddRule.Name = "_bAddRule";
            this._bAddRule.Size = new System.Drawing.Size(122, 23);
            this._bAddRule.TabIndex = 1;
            this._bAddRule.Text = "Add Conditional Rule";
            this._bAddRule.UseVisualStyleBackColor = true;
            this._bAddRule.Click += new System.EventHandler(this._bAddRule_Click);
            // 
            // _bClearRule
            // 
            this._bClearRule.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._bClearRule.Location = new System.Drawing.Point(21, 97);
            this._bClearRule.Name = "_bClearRule";
            this._bClearRule.Size = new System.Drawing.Size(122, 23);
            this._bClearRule.TabIndex = 2;
            this._bClearRule.Text = "Clear Conditional Rule";
            this._bClearRule.UseVisualStyleBackColor = true;
            this._bClearRule.Click += new System.EventHandler(this._bClearRule_Click);
            // 
            // _bViewRules
            // 
            this._bViewRules.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this._bViewRules.Location = new System.Drawing.Point(21, 137);
            this._bViewRules.Name = "_bClearRule";
            this._bViewRules.Size = new System.Drawing.Size(122, 23);
            this._bViewRules.TabIndex = 3;
            this._bViewRules.Text = "Edit Rules for Selection";
            this._bViewRules.UseVisualStyleBackColor = true;
            this._bViewRules.Click += new System.EventHandler(this._bViewRules_Click);
            // 
            // ConditionalRules
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Conditional Rules";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).EndInit();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel1.PerformLayout();
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
        private System.Windows.Forms.Button _bClearRule;
        private System.Windows.Forms.Button _bAddRule;
        private System.Windows.Forms.Button _bViewRules;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.Label _lbOperator;
        private System.Windows.Forms.ComboBox _cbOperator;
    }
}
